﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using WebSimplicity.Data.Helpers;

namespace WebSimplicity.Data
{
    public static class Database
    {
        public static void Install()
        {
            SqlConnection conn = new SqlConnection(Config.GetConnectionString());
            conn.Open();
            RunSqlScript(DataResources.InstallCommon, conn, conn.Database);
            RunSqlScript(DataResources.InstallMembership, conn, conn.Database);
            RunSqlScript(DataResources.InstallProfile, conn, conn.Database);
            RunSqlScript(DataResources.InstallRoles, conn, conn.Database);
            RunSqlScript(DataResources.InstallCore, conn, conn.Database);
            conn.Close();
        }
        public static void RunSqlScript(string Name,SqlConnection conn, params object[] args)
        {
            StringReader sr = new StringReader(Name);
            string[] commands = sr.ReadToEnd().Split(
            new string[] { "GO\r\n", "GO ", "GO\t" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string c in commands)
            {
                SqlCommand comm = new SqlCommand();
                comm.CommandText = string.Format(c, args);
                comm.Connection = conn;
                comm.ExecuteNonQuery();
            }
        }
    }
}
